import os
from hashlib import md5
from comm.tdb import tdb, where
from comm.functons import scan_videos, config


def to_gif(video_path):
    gif_path = config('gif_dir')
    gif_id = md5(video_path.encode('utf-8')).hexdigest()
    gif = gif_path + gif_id + '.gif'
    if tdb.search(where('id') == gif_id):
        if os.path.exists(gif):
            print('已存在', video_path)
            return
        print('重试', video_path)
    cmd = f'ffmpeg -i "{video_path}" -y -r 1000 -t 6 -ss 00:06:01 -vframes 6000 -s 300x186 -f gif "{gif}"'
    os.system(cmd)
    tdb.insert({
        'id': gif_id,
        'src': gif,
        'path': video_path
    })
    print('#' * 150)


if '__main__' == __name__:
    videos = scan_videos(r'E:\迅雷下载', ['mp4'])
    for vp in videos:
        print(vp)
